package com.kanshu.ksgb.fastread.doudou.module.book.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdStyleBean;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.AdUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.retrofit.ADConfigBean;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.ToastUtil;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.ReaderInputParams;
import com.kanshu.ksgb.fastread.doudou.common.business.event.ShelfEvent;
import com.kanshu.ksgb.fastread.doudou.module.book.presenter.BookPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.ReaderJumpConfig;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.base.baseui.SwipeBackActivity.ACTION_DESTORY;

/**
 * Created by wcy on 18/5/27.
 */

public class AdSearchResultHeaderLayout extends LinearLayout {
    @BindView(R.id.ad_container)
    FrameLayout mAdContainer;
    @BindView(R.id.cover)
    ImageView mCover;
    @BindView(R.id.book_title)
    TextView mBookTitle;
    @BindView(R.id.book_desc)
    TextView mBookDesc;
    @BindView(R.id.author)
    TextView mAuthor;
    @BindView(R.id.book_label)
    SuperTextView mBookLabel;
    @BindView(R.id.book_status)
    SuperTextView mBookStatus;
    @BindView(R.id.divider)
    View mDivider;
    @BindView(R.id.big_divider)
    View mBigDivider;
    @BindView(R.id.root_container)
    RelativeLayout mRootContainer;
    @BindView(R.id.search_bottom_container)
    LinearLayout mBottomContainer;
    @BindView(R.id.search_result_join_shelf)
    TextView mJoinShelf;
    @BindView(R.id.bottom_container)
    LinearLayout mBookBottomContainer;
    private BookInfo mBookInfo;
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    BookCityPresenter presenter;

    public AdSearchResultHeaderLayout(Context context) {
        super(context);
        init();
    }

    public AdSearchResultHeaderLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSearchResultHeaderLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        presenter = new BookCityPresenter(lifeCyclerSubject);
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_search_result_header_layout, this);
        ButterKnife.bind(view);
        mDivider.setVisibility(GONE);
    }

    public void hideOrShow(boolean hide) {
        if (hide) {
            DisplayUtils.gone(mRootContainer, mBottomContainer, mBigDivider);
        } else {
            DisplayUtils.visible(mRootContainer, mBottomContainer, mBigDivider);
        }
    }


    public void recycle() {
        AdUtils.destroyAd(mAdContainer);
    }

    public void refreshByBookInfo(BookInfo item) {
        if (item == null) {
            return;
        }
        mBookInfo = item;
        setVisibility(VISIBLE);
        hideOrShow(false);
        GlideImageLoader.load(item.cover_url, mCover);
        mBookTitle.setText(item.book_title);
        mBookDesc.setText(item.book_intro);
        mAuthor.setText(item.author_name);
        mBookLabel.setText(BookUtils.getBookLabel(item));
        mBookStatus.setText(BookUtils.getBookStatus(item));
        DisplayUtils.visible(mAuthor, mBookLabel, mBookStatus, mBookBottomContainer);
        BookUtils.setStatusBg(mBookStatus, item);
        if ("0".equals(mBookInfo.join_bookcase)) {
            mJoinShelf.setText("加入书架");
            mJoinShelf.setEnabled(true);
        } else {
            mJoinShelf.setText("已加入书架");
            mJoinShelf.setEnabled(false);
        }
        AdUtils.fetchAdUtil((Activity) getContext(), mAdContainer, null, ADConfigBean.POSITION_SEARCH_TOP, AdStyleBean.BANNER_STYLE, 0, null);
    }

    public void handleShelfEvent(ShelfEvent event) {
        switch (event.code) {
            case ShelfEvent.ADD_TO_SHELF_SUCCESS:
                BookInfo bookInfo = (BookInfo) event.obj;
                if (mBookInfo != null && TextUtils.equals(bookInfo.book_id, mBookInfo.book_id)) {
                    mJoinShelf.setText("已加入书架");
                    mJoinShelf.setEnabled(false);
                    ToastUtil.showStaticMessage("加入书架成功");
                }
                break;
            case ShelfEvent.DEL_FROM_SHELF_SUCCESS:
                List<String> book_ids = (List<String>) event.obj;
                for (String book_id : book_ids) {
                    if (mBookInfo != null && TextUtils.equals(book_id, mBookInfo.book_id)) {
                        mJoinShelf.setText("加入书架");
                        mJoinShelf.setEnabled(true);
                    }
                }
                break;
            default:
                break;
        }
    }

    public boolean isShowing() {
        return mRootContainer.getVisibility() == View.VISIBLE;
    }

    @OnClick({R.id.search_result_join_shelf, R.id.chapter_list, R.id.start_read})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.search_result_join_shelf:
                if (mBookInfo == null) {
                    return;
                }
                BookPresenter presenter = new BookPresenter(lifeCyclerSubject);
                presenter.joinBookShelf(mBookInfo.book_id);
                break;
            case R.id.chapter_list:
                if (mBookInfo == null) {
                    return;
                }
                ARouterUtils.toActivity(ARouterConfig.BOOK_CHAPTER_LIST, "book_id", mBookInfo.book_id);
                break;
            case R.id.start_read:
                if (mBookInfo == null) {
                    return;
                }
                ReaderInputParams params = new ReaderInputParams();
                params.book_id = mBookInfo.book_id;
                ReaderJumpConfig.startReaderActivity(getContext(), params);
                break;
            default:
                break;
        }
    }

    @OnClick(R.id.root_container)
    public void onViewClicked() {
        if (mBookInfo != null) {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", mBookInfo.book_id);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        lifeCyclerSubject.onNext(ACTION_DESTORY);
    }


}
